<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use LaraPress\Config\LaraPressCms;
use LaraPress\Sliver\LaraPressSingle;

/**
 * WordPress文章资源类
 *
 * 负责将WordPress文章对象转换为标准化的API响应格式
 */
class PostResource extends JsonResource
{
    /**
     * 将资源转换为数组
     *
     * @param Request $request
     * @return array
     */
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->resource->ID,
            'title' => $this->resource->post_title,
            'slug' => $this->resource->post_name,
            'excerpt' => get_the_excerpt($this->resource->ID),
            'status' => $this->resource->post_status,
            'type' => $this->resource->post_type,
            'author' => $this->formatAuthor(),
            'cover' => LaraPressSingle::get_cover($this->resource),
            'images' => LaraPressSingle::get_content_imgs($this->resource),
            'categories' => $this->formatCategories(),
            'tags' => $this->formatTags(),
            'date' => $this->formatDates(),
            'permalink' => get_permalink($this->resource->ID),
            'comment_count' => $this->resource->comment_count,
            'comment_status' => $this->resource->comment_status,
            'row_list_options' => $this->formatListOptions(),
            'actions' => LaraPressSingle::get_actions($this->resource),
        ];
    }

    /**
     * 格式化作者信息
     */
    private function formatAuthor(): array
    {
        $author = get_userdata($this->resource->post_author);

        return [
            'id' => $this->resource->post_author,
            'name' => $author ? $author->display_name : '',
            'slug' => $author ? $author->user_nicename : '',
            'avatar' => get_avatar_url($this->resource->post_author),
        ];
    }

    /**
     * 格式化分类信息
     */
    private function formatCategories(): array
    {
        $categories = [];
        $post_categories = get_the_category($this->resource->ID);

        if ($post_categories) {
            foreach ($post_categories as $category) {
                $categories[] = (new TermResource($category))->toArray(request());
            }
        }

        return $categories;
    }

    /**
     * 格式化标签信息
     */
    private function formatTags(): array
    {
        $tags = [];
        $post_tags = get_the_tags($this->resource->ID);

        if ($post_tags) {
            foreach ($post_tags as $tag) {
                $tags[] = (new TermResource($tag))->toArray(request());
            }
        }

        return $tags;
    }

    /**
     * 格式化日期信息
     */
    private function formatDates(): array
    {
        return [
            'created' => $this->resource->post_date,
            'modified' => $this->resource->post_modified,
        ];
    }

    /**
     * 格式化列表选项
     */
    private function formatListOptions(): array
    {
        return [
            'list_display_data' => LaraPressCms::get_option('list_display_data'),
            'image_position' => LaraPressCms::get_option('image_position'),
            'image_display' => LaraPressCms::get_option('image_display'),
        ];
    }
}
